﻿using System.Collections.Generic;
using System.Linq;

namespace SlamCms.Collections
{
	public class SafeDictionary<TKey, TValue>
	{
		private readonly object _padlock = new object();
		private readonly Dictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();

		public bool Contains(TKey key)
		{
			return _dictionary.ContainsKey(key);
		}

		public TValue this[TKey key]
		{
			get
			{
				lock (_padlock)
				{
					return _dictionary[key];
				}
			}
			set
			{
				lock (_padlock)
				{
					_dictionary[key] = value;
				}
			}
		}

		public bool TryGetValue(TKey key, out TValue value)
		{
			lock (_padlock)
			{
				return _dictionary.TryGetValue(key, out value);
			}
		}

		public bool Remove(TKey key)
		{
			lock (_padlock)
			{
				return _dictionary.Remove(key);
			}
		}

		public IEnumerable<TKey> Keys
		{
			get { return _dictionary.Select(x => x.Key); }
		}
	}
}
